package com.yhd.admin.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yhd.admin.bean.SysMenu;
import com.yhd.admin.service.SysMenuService;
import com.yhd.admin.utils.ServiceMessage;

@Controller
@RequestMapping("/menu")
public class SysMenuController {
	@Autowired
	private SysMenuService sysMenuService;
	
	@RequestMapping()
	public String menuPage(){
		return "menu";
	}
	
	@RequestMapping("/add")
	public String menuAddPage(){
		return "menu-add";
	}
	
	@RequestMapping("/name/check")
	public @ResponseBody ServiceMessage  menuNameCheck(SysMenu menu){
		SysMenu m = sysMenuService.checkSysMenuName(menu);
		return ServiceMessage.ok(m);
	}
	
	@RequestMapping("/add/do")
	public @ResponseBody ServiceMessage  addMenu(SysMenu menu){
		if(StringUtils.isEmpty(menu.getUrl())){
			menu.setUrl(null);
		}
		sysMenuService.addSysMenu(menu);
		return ServiceMessage.ok(menu);
	}
	
	@RequestMapping("/update/{id}")
	public String menuUpdatePage(@PathVariable int id,Model model){
		SysMenu menu  = sysMenuService.getMenuById(id);
		model.addAttribute("menu", menu);
		return "menu-update";
	}
	
	@RequestMapping("/update/do")
	public @ResponseBody ServiceMessage  updateMenu(SysMenu menu){
		if(StringUtils.isEmpty(menu.getUrl())){
			menu.setUrl(null);
		}
		sysMenuService.updateSysMenu(menu);
		return ServiceMessage.ok(menu);
	}
	
	@RequestMapping("/delete")
	public @ResponseBody ServiceMessage  deleteMenu(int id){
		sysMenuService.deleteSysMenuById(id);
		return ServiceMessage.ok();
	}
}
